package com.danskin.wechat.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncryptUtil {

	public static String SHA1(String inStr) {
		MessageDigest md = null;
		String outStr = null;
		try {
			md = MessageDigest.getInstance("SHA-1"); // 选择SHA-1，也可以选择MD5
			byte[] digest = md.digest(inStr.getBytes()); // 返回的是byet[]，要转化为String存储比较方便
			outStr = bytetoString(digest);
		} catch (NoSuchAlgorithmException nsae) {
			nsae.printStackTrace();
		}
		return outStr;
	}

	public static String bytetoString(byte[] digest) {
		String str = "";
		String tempStr = "";

		for (int i = 0; i < digest.length; i++) {
			tempStr = (Integer.toHexString(digest[i] & 0xff));
			if (tempStr.length() == 1) {
				str = str + "0" + tempStr;
			} else {
				str = str + tempStr;
			}
		}
		return str.toLowerCase();
	}

	public static String SHA(String decript) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("SHA");
			digest.update(decript.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 瀛楄妭鏁扮粍杞¬鎹¢涓º 鍗佸叚杩涘埗 鏁
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String MD5(String input) {
		try {
			// 鑾峰緱MD5鎽樿¦佺畻娉曠殑 MessageDigest 瀵硅薄
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 浣跨敤鎸囧畾鐨勫瓧鑺傛洿鏂版憳瑕
			mdInst.update(input.getBytes(Charset.forName("UTF-8")));
			// 鑾峰緱瀵嗘枃
			byte[] md = mdInst.digest();
			// 鎶婂瘑鏂囪浆鎹㈡垚鍗佸叚杩涘埗鐨勫瓧绗︿覆褰㈠紡
			StringBuffer hexString = new StringBuffer();
			// 瀛楄妭鏁扮粍杞¬鎹¢涓º 鍗佸叚杩涘埗 鏁
			for (int i = 0; i < md.length; i++) {
				String shaHex = Integer.toHexString(md[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 鍔犲瘑
	 * 
	 * @param content
	 *            闇€瑕佸姞瀵嗙殑鍐呭®
	 * @param password
	 *            鍔犲瘑瀵嗙爜
	 * @return
	 */
	public static byte[] encryptAES(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 鍒涘缓瀵嗙爜鍣
			byte[] byteContent = content.getBytes("utf8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 鍒濆§嬪寲
			byte[] result = cipher.doFinal(byteContent);
			return result; // 鍔犲瘑
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 瑙ｅ瘑
	 * 
	 * @param content
	 *            寰呰В瀵嗗唴瀹
	 * @param password
	 *            瑙ｅ瘑瀵嗛挜
	 * @return
	 */
	public static byte[] decryptAES(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 鍒涘缓瀵嗙爜鍣
			cipher.init(Cipher.DECRYPT_MODE, key);// 鍒濆§嬪寲
			byte[] result = cipher.doFinal(content);
			return result; // 鍔犲瘑
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * BASE64瑙ｅ瘑
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String decryptBASE64(String key) {

		return "";
	}

	/**
	 * BASE64鍔犲瘑
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(String key) {

		return "";
	}

	public static void main(String[] args) {
		String str1 = "jsapi_ticket=" + "d3yp2Ud9Gzwj6dnSWFhcRVTNI2XUaQOXIs5I-LL9li9tgKzEZj0jwkNsHncb-XI3LwsOIXq8ZVnnq5ik4dtFfg" 
	+ "&noncestr=" + "5143460e8ae644359dc364dc1e3df9c1" + 
				"&timestamp=" + "1508570754" +"&url=" + "http://m.danskin.meijiewd.com/#/storelist";
		String str = "jsapi_ticket=d3yp2Ud9Gzwj6dnSWFhcRVTNI2XUaQOXIs5I-LL9li9tgKzEZj0jwkNsHncb-XI3LwsOIXq8ZVnnq5ik4dtFfg&noncestr=5143460e8ae644359dc364dc1e3df9c1&timestamp=1508570754&url=http://m.danskin.meijiewd.com/";
		String signature = EncryptUtil.SHA1(str);
		
		System.out.println(signature);
		
	}
}